use axum::{Router, Json};
use axum::extract::Query;
use axum::routing::get;
use serde::{Serialize, Deserialize};

use crate::models::{Comment, ModRecommendation};
// use tower_cookies::Cookies;

// use crate::models::User;
// use crate::utils::cookie;

type RouterResult<T> = Result<T,String>;


// struct RouterResponse {

// }

#[derive(Serialize,Deserialize)]
struct DataResponse<T>{
    pub status: &'static str,
    pub data: Vec<T>,
}

impl<T> DataResponse<T> {
    pub fn new(status:&'static str,data: Vec<T>) -> DataResponse<T> {
        DataResponse{
            status: status,
            data: data
        }
    }
}


pub fn router() -> Router {
    Router::new()
        .route("/", get(index))
        .route("/get_all", get(get_all))
        .route("/get_all_comments",get(get_all_comments))
        .route("/forbid",get(forbid_recom))
        .route("/prime",get(prime_recom))
        .route("/active",get(active_recom))
}

async fn index() -> RouterResult<String> {
    Ok("欢迎来到mod推荐页面！".to_string())
}

#[derive(Deserialize,Serialize,Debug)]
struct ChangeModRecomStatus{
    recom_id:i32
}

async fn forbid_recom(Query(query):Query<ChangeModRecomStatus>) -> Json<bool> {
    if let Ok(_) = ModRecommendation::forbid_by_id(query.recom_id).await{
        return Json(true)
    }
    Json(false)
}
async fn prime_recom(Query(query):Query<ChangeModRecomStatus>) -> Json<bool> {
    if let Ok(_) = ModRecommendation::prime_by_id(query.recom_id).await{
        return Json(true)
    }
    Json(false)
}
async fn active_recom(Query(query):Query<ChangeModRecomStatus>) -> Json<bool> {
    if let Ok(_) = ModRecommendation::active_by_id(query.recom_id).await{
        return Json(true)
    }
    Json(false)
}

async fn get_all() -> Json<Vec<ModRecommendation>> {

    let m_rec = ModRecommendation::fetch_all().await.unwrap();


    Json(m_rec)

}

#[derive(Serialize,Deserialize,Debug)]
struct GetModComment{
    recom_id:i32
}
/// 获取Mod推荐贴的所有评论
async fn get_all_comments(Query(query):Query<GetModComment>) -> Json<Vec<Comment>> {

    // let recom = ModRecommendation::fetch_one_by_id(query.recom_id).await.unwrap();
    let comments = ModRecommendation::fetch_comments_of(query.recom_id).await.unwrap();
    Json(comments)

}